/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.CreatureDTO;
import cz.muni.fi.pa165.creatures.dto.RegionDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.Creature;
import cz.muni.fi.pa165.creatures.model.Region;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Region} and
 * {@link cz.muni.fi.pa165.creatures.dto.RegionDTO} and vice versa.
 *
 * @author smikloso
 */
public class RegionMapping implements MappingDTO<RegionDTO, Region> {

    @Autowired
    private CreatureMapping creatureMapping;

    @Override
    public RegionDTO entityToDTO(Region region) {
        RegionDTO dto = new RegionDTO();
        dto.setId(region.getId().toString());
        dto.setName(region.getName());
        dto.setDescription(region.getDescription());
        dto.setArea(region.getArea().toString());
        if (region.getCreatures() != null && !region.getCreatures().isEmpty()) {
            List<CreatureDTO> creatures = new ArrayList<CreatureDTO>();
            for (Creature creature : region.getCreatures()) {
                creatures.add(creatureMapping.entityToDTO(creature));
            }
            dto.setCreatures(creatures);
        }
        return dto;
    }

    @Override
    public Region DTOtoEntity(RegionDTO dto) {
        Region region = new Region();
        region.setId(Long.parseLong(dto.getId()));
        region.setName(dto.getName());
        region.setArea(Long.parseLong(dto.getArea()));
        region.setDescription(dto.getDescription());
        if (dto.getCreatures() != null && !dto.getCreatures().isEmpty()) {
            for (CreatureDTO creatureDTO : dto.getCreatures()) {
                region.addCreature(creatureMapping.DTOtoEntity(creatureDTO));
            }
        }
        return region;
    }
}
